<template>
 <div class="e-form">
    <div class="tabs">
        <el-tabs tab-position="left" v-model="tabsName" @tab-click="onChangeTab">
            <el-tab-pane label="基本信息" name="baseInfo" :disabled="clickTabFlag">
            </el-tab-pane>
            <el-tab-pane label="清单信息" name="contractList" :disabled="clickTabFlag">
            </el-tab-pane>
            <el-tab-pane label="附件信息" name="attach" :disabled="clickTabFlag">
            </el-tab-pane>
            <el-tab-pane label="审核历史" name="auditHistory" :disabled="clickTabFlag">
            </el-tab-pane>
            <div>
                <!-- 基本信息 -->
                <div id="baseInfoCon" class="con">
                    <div class="tabs-title" id="baseInfo">基本信息</div>
                    <div style="width: 100%" class="form" v-if="objFlag.flag1">
                        <el-form :model="formData" :label-width="formLabelWidth">
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="合同编号：">
                                        <span>{{formData.ctCode}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="所属机构：">
                                        <span>{{formData.organization}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="变更编号：">
                                        <span>{{}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="变更日期：">
                                        <span>{{}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="合同名称：">
                                        <el-input v-model="formData.ctName" autocomplete="off"></el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="变更金额：">
                                        <span>{{}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="原不含税金额(元)：">
                                        <span>{{}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="变更后不含税金额：">
                                        <span>{{}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="变更次数：">
                                        <span>{{}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="税率%：">
                                        <el-input v-model="formData.taxRate"  v-enter-number autocomplete="off"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="税额(元)：">
                                        <el-input v-model="formData.taxAmount" v-enter-number autocomplete="off"></el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="批复状态：">
                                        <span>{{}}</span>
                                        <!-- <el-select v-model="formData.currency" placeholder="请选择">
                                            <el-option
                                                v-for="(
                                                    item, index
                                                ) in options.currencyForm"
                                                :key="index"
                                                :label="item"
                                                :value="index"
                                            >
                                            </el-option>
                                        </el-select> -->
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="原合同净价(元)：">
                                        <span>{{}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="变更后合同净价(元)：">
                                        <span>{{}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="原暂列金额(元)：">
                                        <span>{{}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="变更后暂列金额(元)：">
                                        <span>{{}}</span>
                                        <!-- <el-input v-model="formData.taxAmount" v-enter-number autocomplete="off"></el-input> -->
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="原合同金额(元)：">
                                        <span>{{}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="变更后合同金额(元)：">
                                        <span>{{}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="24">
                                    <el-form-item label="变更(索赔)理由及方案简述：">
                                        <el-input type="textarea" v-model="formData.ctPrimarycoverage" autocomplete="off"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-form>
                    </div>
                    <div class="seize-a-seat" v-else>
                        <i class="el-icon-loading"></i>
                    </div>
                </div>

                <!-- 清单信息 -->
                <div id="contractListCon" class="con">
                    <div class="tabs-title" id="contractList">清单信息</div>
                    <div class="e-table" v-if="objFlag.flag2">
                        <div class="top">
                            <div class="left">
                                <div>
                                    <el-button size="small" type="primary" plain>清单模板库</el-button>
                                    <el-button size="small" type="primary" plain>分部分项挂清单</el-button>
                                </div>
                            </div>
                        </div>
                        <el-table
                            :data="contractListTable"
                            border
                            style="width: 100%"
                        >
                            <el-table-column  prop="" label="细目编号" width="200">
                            </el-table-column>
                            <el-table-column
                                prop=""
                                label="细目名称"
                                width="200"
                            >
                            </el-table-column>
                            <el-table-column
                                prop=""
                                label="计量单位"
                                width="100"
                            >
                            </el-table-column>
                            <el-table-column
                                prop=""
                                label="原数量"
                                width="100"
                            >
                            </el-table-column>
                            <el-table-column
                                prop=""
                                label="原单价(元)"
                                width="100"
                            >
                            </el-table-column>
                            <el-table-column
                                prop=""
                                label="原金额(元)"
                                width="100"
                            >
                            </el-table-column>
                            <el-table-column
                                prop=""
                                label="截止上期变更后数量"
                                width="200"
                            >
                            </el-table-column>
                            <el-table-column
                                prop=""
                                label="截止上期变更后金额"
                                width="200"
                            >
                            </el-table-column>
                        </el-table>
                    </div>
                    <div class="seize-a-seat" v-else>
                        <i class="el-icon-loading"></i>
                    </div>
                </div>

                <!-- 附件信息 -->
                <div id="attachCon" class="con">
                    <div class="tabs-title" id="attach">附件信息</div>
                    <div class="e-table" v-if="objFlag.flag3">
                        <div class="top">
                            <div class="left">
                                <div>
                                    <el-button size="small" type="primary" plain>下载</el-button>
                                    <el-button size="small" type="danger" plain>删除</el-button>
                                </div>
                            </div>
                        </div>
                        <el-table
                            :data="attachTable"
                            border
                            style="width: 100%"
                        >
                            <el-table-column type="selection" width="55">
                            </el-table-column>
                            <el-table-column  label="序号" type="index" width="200">
                            </el-table-column>
                            <el-table-column
                                prop="enclosureName"
                                label="文件名称"
                                width="200"
                            >
                            </el-table-column>
                            <el-table-column
                                prop="enclosureSize"
                                label="文件大小"
                                width="200"
                            >
                            </el-table-column>
                            <el-table-column
                                prop="gmtCreate"
                                label="上传时间"
                                width="200"
                            >
                            </el-table-column>
                            <el-table-column
                                prop="uploadName"
                                label="上传人"
                            >
                            </el-table-column>
                        </el-table>
                        <div class="upload">
                            <el-upload
                                class="upload-demo"
                                drag
                                action="https://jsonplaceholder.typicode.com/posts/"
                                multiple
                            >
                                <el-button
                                    type="primary"
                                    style="padding: 20px 80px"
                                    >上传附件</el-button
                                >
                                <div style="margin-top: 10px; font-size: 12px">
                                    鼠标拖入上传，按住Ctrl可多选附件
                                </div>
                            </el-upload>
                        </div>
                    </div>
                    <div class="seize-a-seat" v-else>
                        <i class="el-icon-loading"></i>
                    </div>
                </div>

                <!-- 审核历史 -->
                <div id="auditHistoryCon" class="con">
                    <div class="tabs-title" id="auditHistory">审核历史</div>
                    <div class="e-table" v-if="objFlag.flag4">
                        <el-table
                            :data="auditHistoryTable"
                            border
                            style="width: 100%"
                        >
                            <el-table-column prop="auditLevel" label="审核级次" width="150">
                            </el-table-column>
                            <el-table-column prop="examineName" label="审核人" width="150">
                            </el-table-column>
                            <el-table-column
                                prop="examinePost"
                                label="审核人岗位"
                                width="150"
                            >
                            </el-table-column>
                            <el-table-column prop="notificationTime" label="通知时间" width="150">
                            </el-table-column>
                            <el-table-column prop="gmtCreate" label="审核时间" width="150">
                            </el-table-column>
                            <el-table-column prop="examineOpinion" label="审核意见">
                            </el-table-column>
                        </el-table>
                    </div>
                    <div class="seize-a-seat" v-else>
                        <i class="el-icon-loading"></i>
                    </div>
                </div>

            </div>
        </el-tabs>
    </div>
    <div class="buttons">
        <el-button type="primary" size="small" style="background: #2e61d7"
            >打印</el-button
        >
        <el-button type="primary" size="small" style="background: #2e61d7"
            >审核</el-button
        >
        <el-button type="primary" size="small" style="background: #2e61d7" @click="addSave"
            >保存</el-button
        >
        <el-button type="primary" size="small" style="background: #2e61d7"
            >保存并提交</el-button
        >
        <el-button  type="danger" size="small"
            >撤回</el-button
        >
        <el-button  type="danger" size="small"
        @click="ctDelete"
            >删除</el-button
        >
        <el-button type="danger" size="small"
            >作废</el-button
        >
        <el-button size="small" @click="handleClose">关闭</el-button>
    </div>
    <div id="seat" :style="{height: seatHeight + 'px'}"></div>
 </div>
</template>

<script>
import '@/utils/jquery.scrollTo.min'
import { mapState } from 'vuex'
import fn from '@/utils/checkEmpty.js'
import ctleaseIncome from '@/api/balanceContractApi/ctleaseIncomApi/ctleaseIncomeSigning.js'
export default {
    data () {
        return {
            //基本信息表单数据
            formData: {
                'advanceCharge': null,
                'aname': '',
                'baseCurrency': null,
                'baseRate': null,
                'bcAdvanceCharge': null,
                'bcCtMoney': null,
                'bcPerformanceBond': null,
                'bname': '',
                'changeAdvanceCharge': null,
                'changeCode': '',
                'changeCtMoney': null,
                'changeNumber': null,
                'ctAddressCode': '',
                'ctCode': '',
                'ctMoney': null,
                'ctName': '',
                'ctPrimarycoverage': '',
                'ctState': null,
                'ctType': null,
                'ctsubcontractId': '',
                'currency': null,
                'financialSharing': null,
                'freightTaxRate': null,
                'gmtCreate': '',
                'gmtModified': '',
                'guaranteedAmount': null,
                'handler': '',
                'importId': '',
                'importName': '',
                'intoMoney': null,
                'isDelete': null,
                'isPerformanceBond': null,
                'isTax': null,
                'leasePlan': '',
                'notTaxprice': null,
                'nullifyCreated': '',
                'nullifyCreator': '',
                'nullifyCreatorId': '',
                'nullifyDesciption': '',
                'nullifyReason': '',
                'orgId': '',
                'organization': '',
                'outMoney': null,
                'rmbRate': null,
                'signatory': '',
                'signatoryAddress': '',
                'signingDate': '',
                'supplier': '',
                'taxAmount': null,
                'taxRate': null,
                'transportationCost': null,
                'userId': '',
                'validity1': '',
                'validity2': ''
            },
            //主体信息表单数据
            mainInfo: {
                'aaddress': '',
                'abank': '',
                'accountNumber': '',
                'afax': '',
                'aname': '',
                'aphone': '',
                'apostalCode': '',
                'aprincipalAgent': '',
                'arepresentative': '',
                'baccountNumber': '',
                'baddress': '',
                'bbank': '',
                'bbiddingName': '',
                'bcreditCode': '',
                'bfax': '',
                'bname': '',
                'bphone': '',
                'bpostalCode': '',
                'bprincipalAgent': '',
                'brepresentative': '',
                'caccountNumber': '',
                'caddress': '',
                'cbank': '',
                'ccreditCode': '',
                'cname': '',
                'cphone': '',
                'cpostalCode': '',
                'cprincipalAgent': '',
                'crepresentative': '',
                'ctsubcontractId': '',
                'ctsubjectId': '',
                'fairBank': '',
                'fairDate': '',
                'fairNumber': '',
                'fairOpinion': '',
                'gmtCreate': '',
                'gmtModified': '',
                'impartialOrgan': '',
                'isDelete': 0
            },
            formDatas: {
                'ctConveyanceContract': {
                    'advanceCharge': null,
                    'aname': '',
                    'applicationStatus': '',
                    'baseCurrency': null,
                    'baseRate': null,
                    'bcAdvanceCharge': null,
                    'bcCtMoney': null,
                    'bcPerformanceBond': null,
                    'bname': '',
                    'cashPledge': null,
                    'cashPledges': null,
                    'changeAdvanceCharge': null,
                    'changeCode': '',
                    'changeCtMoney': null,
                    'changeNumber': null,
                    'ctAddressCode': '',
                    'ctCode': '',
                    'ctMoney': null,
                    'ctName': '',
                    'ctPrimarycoverage': '',
                    'ctState': null,
                    'ctType': null,
                    'ctsubcontractId': '',
                    'financialSharing': null,
                    'freightTaxRate': null,
                    'gmtCreate': '',
                    'gmtModified': '',
                    'guaranteedAmount': null,
                    'handler': '',
                    'importId': '',
                    'importName': '',
                    'intoMoney': null,
                    'isDelete': null,
                    'isPerformanceBond': null,
                    'isTax': null,
                    'leasePlan': '',
                    'notTaxprice': null,
                    'nullifyCreated': '',
                    'nullifyCreator': '',
                    'nullifyCreatorId': '',
                    'nullifyDesciption': '',
                    'nullifyReason': '',
                    'orgId': '',
                    'organization': '',
                    'outMoney': null,
                    'signatory': '',
                    'signatoryAddress': '',
                    'signingDate': '',
                    'supplier': '',
                    'taxAmount': null,
                    'taxRate': null,
                    'transportationCost': null,
                    'userId': '',
                    'validity1': '',
                    'validity2': ''
                },
                'ctConveyanceEnclosures': [
                    {
                        'ctId': '',
                        'ctsubcontractId': '',
                        'defaultValue': null,
                        'enclosureId': '',
                        'enclosureName': '',
                        'enclosureSize': null,
                        'gmtCreate': '',
                        'gmtModified': '',
                        'isDelete': null,
                        'isFormal': null,
                        'uploadId': '',
                        'uploadName': ''
                    }
                ],
                'ctConveyanceLists': [
                    {
                        'amountMoney': null,
                        'approvalMoney': null,
                        'approvalPrice': null,
                        'approvalQuantity': null,
                        'ctId': '',
                        'ctsubcontractId': '',
                        'defaultValue': null,
                        'endTime': '',
                        'gmtCreate': '',
                        'gmtModified': '',
                        'isDelete': null,
                        'leaseTime': '',
                        'mateialCategory': '',
                        'materialCode': '',
                        'materialName': '',
                        'quantity': null,
                        'reviewComments': '',
                        'serialNumber': null,
                        'specifications': '',
                        'startTime': '',
                        'textureMaterial': '',
                        'timingUnit': '',
                        'unitPrice': null
                    }
                ],
                'ctConveyanceSubject': {
                    'aaddress': '',
                    'abank': '',
                    'accountNumber': '',
                    'afax': '',
                    'aname': '',
                    'aphone': '',
                    'apostalCode': '',
                    'aprincipalAgent': '',
                    'arepresentative': '',
                    'baccountNumber': '',
                    'baddress': '',
                    'bbank': '',
                    'bbiddingName': '',
                    'bcreditCode': '',
                    'bfax': '',
                    'bname': '',
                    'bphone': '',
                    'bpostalCode': '',
                    'bprincipalAgent': '',
                    'brepresentative': '',
                    'caccountNumber': '',
                    'caddress': '',
                    'cbank': '',
                    'ccreditCode': '',
                    'cname': '',
                    'cphone': '',
                    'cpostalCode': '',
                    'cprincipalAgent': '',
                    'crepresentative': '',
                    'ctsubcontractId': '',
                    'ctsubjectId': '',
                    'defaultValue': null,
                    'fairBank': '',
                    'fairDate': '',
                    'fairNumber': '',
                    'fairOpinion': '',
                    'gmtCreate': '',
                    'gmtModified': '',
                    'impartialOrgan': '',
                    'isDelete': null
                },
                'officialDocuments': [
                    {
                        'ctId': '',
                        'ctsubcontractId': '',
                        'defaultValue': null,
                        'enclosureId': '',
                        'enclosureName': '',
                        'enclosureSize': null,
                        'gmtCreate': '',
                        'gmtModified': '',
                        'isDelete': null,
                        'isFormal': null,
                        'uploadId': '',
                        'uploadName': ''
                    }
                ]
            },
            formLabelWidth: '190px',
            tableData1: [],
            tabsName: 'baseInfo',
            screenWidth: 0,
            screenHeight: 0,
            lastConHeight: 0,
            clickTabFlag: false, // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            ctsubcontractId: '', //合同id
            attachTable: [], //附件信息表格数据
            auditHistoryTable: [], //审核历史表格数据
            contractListTable: [], //清单信息表格数据
            ctAddressCode: [],
            aAddressCode: [],  //甲方地址
            bAddressCode: [],  //乙方地址
            cAddressCode: [],  //运输单位地址
            objFlag: {   //判断是否转圈圈
                flag1: false,
                flag2: false,
                flag3: false,
                flag4: false,
            },
        }
    },
    mounted () {
        this.getBasic()
        // 获取最后一个内容区域的高度，计算底部空白
        this.getLastConHeight()
        // 保存所有tabName
        const arr = ['baseInfo', 'contractList', 'attach', 'auditHistory']

        let $idsTop = []
        window.addEventListener('scroll', () => {
            // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            if (this.clickTabFlag) {
                return
            }

            if (!$idsTop[$idsTop.length - 1]) {
                $idsTop = arr.map(item => {
                    const itemTop = document.getElementById(item).offsetTop
                    return itemTop
                })
            }
            const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
            // 倒序查找
            let curLocal = 0
            for (let len = $idsTop.length, i = len - 1; i >= 0; i--) {
                let item = $idsTop[i]
                if ((scrollTop + 1) >= item) {
                    curLocal = i
                    break
                }
            }
            // 设置对应tabName
            this.tabsName = arr[curLocal]
            this.queryData(this.tabsName)
        })

        this.screenWidth = document.documentElement.clientWidth
        this.screenHeight = document.documentElement.clientHeight
        window.addEventListener('resize', () => {
            this.screenWidth = document.documentElement.clientWidth
            this.screenHeight = document.documentElement.clientHeight
            $idsTop = arr.map(item => {
                const itemTop = document.getElementById(item).offsetTop
                return itemTop
            })
        })
    },
    computed: {
        ...mapState({
            options: state => state.contract.ctClassify
        }),
        // 填补底部空白，以使高度够滚动
        seatHeight () {
            return this.screenHeight - 72 - this.lastConHeight
        }
    },
    methods: {
        //更新数据
        addSave () {
            if(this.ctAddressCode.length > 0) {
                this.formData.ctAddressCode = this.ctAddressCode.join('-') //行政编码
            }
            this.formData.ctsubcontractId = this.$route.query.id
            this.formDatas.ctConveyanceContract = this.formData
            this.$Ajax.httpPost({
                url: '/contract/CtConveyancePsdreparationd/updateAll',
                params: this.formDatas
            }).then(res => {
                this.$message({
                    message: res.message,
                    type: 'success'
                })
            })
        },
        //取消
        handleClose () {
            this.$router.push('/landlordChangeContract')
        },
        // 获取地址对应中文
        getAddrLabel (val) {
            this.formData.signatoryAddress = val.join('-')
        },
        //获取甲方地址对应中文
        getaAddrLabel (val) {
            this.mainInfo.aaddress = val.join('-')
        },
        //获取乙方方地址对应中文
        getbAddrLabel (val) {
            this.mainInfo.baddress = val.join('-')
        },
        //判断是否为空
        checkEmpty (datas) {
            return fn.checkEmpty(datas)
        },
        //根据点击不同的tab,调用不同的接口
        queryData (name) {
            switch(name) {
            case 'baseInfo':
                if(this.checkEmpty(this.formData)) this.getBasic()
                break
            case 'contractList':
                if(this.checkEmpty(this.contractListTable)) this.getContractList()
                break
            case 'attach':
                if(this.checkEmpty(this.attachTable)) this.getAttach()
                break
            case 'auditHistory':
                if(this.checkEmpty(this.auditHistoryTable)) this.getAuditHistory()
                break
            }
        },
        //根据id查询基本信息
        getBasic () {
            this.ctsubcontractId = this.$route.query.id
            ctleaseIncome.findByIdBasic(this.ctsubcontractId).then(res=>{
                if(res.ctAddressCode) {
                    this.ctAddressCode = res.ctAddressCode.split('-')
                }
                this.formData = res
                this.objFlag.flag1 = true
            })
        },
        //根据id查询主体信息
        getContractList () {
            this.objFlag.flag2 = true
        },
        //根据id查询附件信息
        getAttach () {
            ctleaseIncome.findByIdEnclosure(this.ctsubcontractId).then(res=>{
                this.attachTable = res
                this.objFlag.flag3 = true
            })
        },
        //根据id审核历史
        getAuditHistory () {
            ctleaseIncome.findByIdExamine(this.ctsubcontractId).then(res=>{
                this.auditHistoryTable = res
                this.objFlag.flag4 = true
            })
        },
        //删除数据
        ctDelete () {
            this.$Ajax.httpPost({
                url: '/contract/CtConveyancePsdreparationd/deleteById',
                params: {
                    ctsubcontractId: this.ctsubcontractId,
                }
            }).then(res => {
                console.log(res)
                if(res.code == 200) {
                    this.$message({
                        message: '删除成功',
                        type: 'success'
                    })
                    this.$router.push('/landlordChangeContract')
                }
            })
        },
        onChangeTab (e) {
            $.scrollTo(`#${e.name}`, 500)
            // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            this.clickTabFlag = true
            // 动画结束后，恢复状态
            setTimeout(() => {
                this.clickTabFlag = false
            }, 600)
            this.queryData(e.name)
        },
        // 获取最后一个内容区域的高度，计算底部空白
        getLastConHeight () {
            let si = setInterval(() => {
                // 因为dom异步加载，通过轮询找到渲染后的dom，获取高度
                if (document.getElementById('recodesCon')) {
                    const lastConHeight = document.getElementById('recodesCon').offsetHeight
                    console.log('lastConHeight: ', lastConHeight)
                    this.lastConHeight = lastConHeight
                    clearInterval(si)
                    si = null
                }
            }, 100)
        },
    },
    components: {
    }
}

</script>

<style lang='scss' scoped>
.mainTitle{
    box-sizing: border-box;
    width:100%;
    height: 40px;
    line-height: 40px;
    background: rgb(246,246,246);
    border: 1px solid rgb(236, 236, 236);
    margin: auto;
    margin-bottom: 15px;
    padding-left: 10px;
}
.separ{
   width: 30px;
   height: 40px;
   line-height: 18px;
   text-align: center;
}
.e-table{
    min-height: auto;
    background: #fff;
}
.upload {
    margin: 20px auto;
    display: flex;
    justify-content: center;
    text-align: center;
}

.upload-demo {
    display: flex;
    justify-content: center;
    align-items: center;
}

::v-deep.el-input--suffix .el-input__inner {
    padding-right: 5px;
}
::v-deep.el-table .cell {
    text-align: center;
}
</style>